/*******************************************************************************
*  file    : modhertzelwindow.cpp
*  created : 22.05.2012
*  author  : Slyshyk Oleksiy (alex312@meta.ua)
*******************************************************************************/

#include "modhertzelwindow.hpp"
#include "ui_qmodhertzelwindow.h"
#include <cmath>

QModHertzelWindow::QModHertzelWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QModHertzelWindow),
    m_sampleRate(1)
{
    ui->setupUi(this);

    connect(ui->pushButton, SIGNAL(clicked()), this, SIGNAL(recalcHertzel()) );

    connect(ui->bufSizeSpin, SIGNAL(valueChanged(int)), this, SLOT(bufSizeChange(int)) );
    connect(ui->HarmonicSpin, SIGNAL(valueChanged(int)), this, SLOT(HarmonicChange(int)) );
}

QModHertzelWindow::~QModHertzelWindow()
{
    delete ui;
}

int QModHertzelWindow::bufSize()
{
    return ui->bufSizeSpin->value();
}

int QModHertzelWindow::harmonikNum()
{
    return ui->HarmonicSpin->value();
}

void QModHertzelWindow::setSampleRate(int rate)
{
    m_sampleRate = rate;
    ui->sampleRateLabel->setText(QString(tr("Частота дискретизации : %1")).arg(m_sampleRate));
    double f = ui->HarmonicSpin->value() * m_sampleRate / ui->bufSizeSpin->value();
    ui->frequenciLabel->setText(QString(tr("Частота : %1")).arg(f));
}

void QModHertzelWindow::bufSizeChange(int new_size)
{
    if( ! is_program_change )
        {
            is_program_change = true;
            ui->deltaFSpin->setValue( (double)m_sampleRate/new_size);

            double f = ui->HarmonicSpin->value() * m_sampleRate / new_size;
            ui->frequenciLabel->setText(QString(tr("Частота : %1")).arg(f));
        }
    else
        {
            is_program_change = false;
        }

}

void QModHertzelWindow::HarmonicChange(int new_harm)
{
    double f = new_harm * m_sampleRate / ui->bufSizeSpin->value();
    ui->frequenciLabel->setText(QString(tr("Частота : %1")).arg(f));
}

void QModHertzelWindow::deltaFChange(double new_delta)
{
    if( ! is_program_change )
        {
            is_program_change = true;
            ui->bufSizeSpin->setValue( ceil((double)m_sampleRate/new_delta));
        }
    else
        {
            is_program_change = false;
        }
}

